package group.program.Service.impl;
import group.program.Service.DocxService;
import org.apache.poi.xwpf.usermodel.*;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
@Service
public class DocxServiceImpl implements DocxService {

    @Override
    public String extractInformation(MultipartFile file) {
        StringBuilder content = new StringBuilder();

        try (InputStream is = file.getInputStream(); XWPFDocument document = new XWPFDocument(is)) {
            // 提取段落内容
            for (XWPFParagraph paragraph : document.getParagraphs()) {
                content.append(paragraph.getText()).append("\n");
            }

            // 提取表格内容
            for (XWPFTable table : document.getTables()) {
                for (XWPFTableRow row : table.getRows()) {
                    for (XWPFTableCell cell : row.getTableCells()) {
                        content.append(cell.getText()).append("\t"); // 使用制表符分隔单元格内容
                    }
                    content.append("\n"); // 换行以分隔行
                }
                content.append("\n"); // 换行以分隔表格
            }
        } catch (IOException e) {
            e.printStackTrace();
            return "Failed to process the file.";
        }

        return content.toString();
    }
}
